package uk.co.hs.helper;

import java.math.BigDecimal;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;

public abstract class HsHelper
{
  /**
   * Date comparison algorithm to determine whether an object is current based on the current date
   * in relation to given effective-from date and effective-to dates.
   *
   * Everything is converted to 'date midnight' first.
   *
   * @param fromDate
   * @param toDate
   * @return true or false
   */
  public static final boolean isCurrent(DateTime aFromDate,
                                        DateTime aToDate)
  {
    DateMidnight currentDate = new DateMidnight();
    if (aToDate != null
        && (currentDate.isAfter(aFromDate.toDateMidnight()) || currentDate.isEqual(aFromDate.toDateMidnight()))
        && (currentDate.isBefore(aToDate.toDateMidnight()) || currentDate.isEqual(aToDate.toDateMidnight())))
    {
      return true;
    }
    else if (aToDate == null
             && (currentDate.isAfter(aFromDate.toDateMidnight()) || currentDate.isEqual(aFromDate.toDateMidnight())))
    {
      return true;
    }
    return false;
  }

  public static Boolean stringsMatch(String aA, String aB)
  {
    // Convert strings to deal with differences between SQL server and ORACLE
    String convertedA = aA;
    if (aA == null)
    {
      convertedA = "";
    }

    String convertedB = aB;
    if (aB == null)
    {
      convertedB = "";
    }

    if (convertedA.equals(convertedB))
    {
      return Boolean.TRUE;
    }

    return Boolean.FALSE;
  }

  public static Boolean charactersMatch(Character aA, Character aB)
  {
    if ((aA == null && aB != null)
        || (aA != null && aB == null)
        || (aA != null && aB != null && !aA.equals(aB)))
    {
      return Boolean.FALSE;
    }
    return Boolean.TRUE;
  }

  public static Boolean integersMatch(Integer aA, Integer aB)
  {
    if ((aA == null && aB != null)
        || (aA != null && aB == null)
        || (aA != null && aB != null && !aA.equals(aB)))
    {
      return Boolean.FALSE;
    }
    return Boolean.TRUE;
  }

  public static Boolean bigDecimalsMatch(BigDecimal aA, BigDecimal aB)
  {
    if ((aA == null && aB != null)
        || (aA != null && aB == null)
        || (aA != null && aB != null && aA.compareTo(aB) != 0))
    {
      return Boolean.FALSE;
    }
    return Boolean.TRUE;
  }

  public static Boolean dateTimesMatch(DateTime aA, DateTime aB)
  {
    if ((aA == null && aB != null)
        || (aA != null && aB == null)
        || (aA != null && aB != null && !aA.isEqual(aB)))
    {
      return Boolean.FALSE;
    }
    return Boolean.TRUE;
  }
}